local PSO = require "PSO"

local PSOEx = PSO:new( {
      boundary_type = 1;                    --边界
      mutation_factor =  0.1;               --变异因子，每次迭代有10%的概率变异
      inertia_max = 0.9;                      --最大惯量
      inertia_min  =0.4;                       --最小惯量
})


----------------------------------------------------------------------------------------------------------------------------------------
--初始化速度
function PSOEx:initVelocityFun(id_par)	
	return self.max_vel*(self.par[id_par].pmax-self.par[id_par].pmin) * (2*math.random()-1);   --初始化速度位置
end
----------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------
--初始化给定样本的速度
function PSOEx:initSampleVelocityFun(id_par,vel)	
	return vel or self.max_vel*0.2*(self.par[id_par].pmax-self.par[id_par].pmin) * (2*math.random()-1);          --初始化速度 initial velocity
end
----------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------
--- 惯量函数，随迭代次数变化的惯量
function PSOEx:inertiaFun()
	return self.inertia_max - ( self.inertia_max - self.inertia_min )* ( self.num_iter - 1) / ( self.iterations - 1)
end
----------------------------------------------------------------------------------------------------------------------------------------


-------------------------------------------------------------------------------------------------
--变异
function PSOEx:mutation()
	for jj = 1,self.swarm_size do
		if math.random()<self.mutation_factor then
			mutation_pos = math.ceil(math.random()*#self.par)
			self.swarm[jj][mutation_pos].pos = math.random()*(self.par[mutation_pos].pmax-self.par[mutation_pos].pmin)+self.par[mutation_pos].pmin  
		end
	end
end
-------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------
--增加了变异，修改了边界函数，初始化速度
function PSOEx:step()	
	self.num_iter = self.num_iter+1;	 	
	print( "num_iter:",self.num_iter)
		
	----------------------------------------------------------------------------------------------------------------------------------------
	--envaluation 
	 self:evaluate()
	----------------------------------------------------------------------------------------------------------------------------------------

	
	----------------------------------------------------------------------------------------------------------------------------------------
	--update  individual and global best position and val
	self:update_best_pos_and_val()
	----------------------------------------------------------------------------------------------------------------------------------------
	
	----------------------------------------------------------------------------------------------------------------------------------------
	--- updating velocity vectors
	self:update_velocity()
	----------------------------------------------------------------------------------------------------------------------------------------

	----------------------------------------------------------------------------------------------------------------------------------------
	 --update  position 
	 self:update_position()
	 self:boundary_process()
	----------------------------------------------------------------------------------------------------------------------------------------
	
	----------------------------------------------------------------------------------------------------------------------------------------
	--mutation
	self:mutation()
	----------------------------------------------------------------------------------------------------------------------------------------
end
-------------------------------------------------------------------------------------------------


return PSOEx;